<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="流转人" prop="landPerson">
        <el-input
          v-model="queryParams.landPerson"
          placeholder="请输入流转人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流转面积" prop="landArea">
        <el-input
          v-model="queryParams.landArea"
          placeholder="请输入流转面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['land:reginfo:query']" type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['land:reginfo:add']"
          type="primary"
          plainindex.vue
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['land:reginfo:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流转人" align="center" prop="landPerson" />
      <el-table-column label="流转面积" align="center" prop="landArea" />
      <el-table-column label="流转区域" align="center" prop="district" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="信息状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_land_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="审核信息" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_land_audit" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['land:reginfo:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['land:reginfo:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="流转人" prop="landPerson">
          <el-input v-model="form.landPerson" placeholder="请输入流转人" />
        </el-form-item>
        <el-form-item label="流转面积" prop="landArea">
          <el-input v-model="form.landArea" placeholder="请输入流转面积" />
        </el-form-item>
        <el-form-item label="流转位置信息" prop="lat" @click="openVisible = true">
          <div style="border-radius: 5px ; border:1px solid #b4bccc; padding:0 15px ;height: 36px; line-height: 36px;" @click="chooseOpen">{{ form.lat ? form.lat+','+form.lng:'请选择坐标' }}</div>
        </el-form-item>
        <el-form-item label="流转区域" prop="district">
          <el-input v-model="form.district" placeholder="请输入流转区域" />
        </el-form-item>
        <el-form-item label="流转性质" prop="natureId">
          <el-select v-model="form.natureId" placeholder="请选择" clearable class="nature-circulation">
            <el-option
              v-for="(dict) in natureCirculationArr"
              :key="dict.natureId"
              :label="dict.natureName"
              :value="dict.natureId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="信息状态修改" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.sys_land_state"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核信息" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus">
            <el-radio
              v-for="dict in dict.type.sys_land_audit"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="坐标位置" :visible.sync="openVisible" width="900px" append-to-body>
      <div class="map">
        <div class="box">当前坐标：{{ lat }},{{ lng }}</div>
        <div ref="mapView" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="choosePosition">确 定</el-button>
        <el-button @click="openVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, addPost, getPost, updatePost, delPost, getNature } from '@/api/land/reginfo'
import { getToken } from '@/utils/auth'

export default {
  name: 'News',
  dicts: ['sys_land_state', 'sys_land_audit'],
  data() {
    return {
      baseEnv: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '新闻新增',
      // 是否显示弹出层
      open: false,
      openVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        state: undefined,
        auditStatus: undefined
      },
      // 表单校验
      rules: {
        landPerson: [
          { required: true, message: '请输入流转人', trigger: 'blur' }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],
        lat: [
          { required: true, message: '坐标不能为空', trigger: 'blur' }
        ],
        natureId: [
          { required: true, message: '流转性质不能为空', trigger: 'blur' }
        ]
      },
      baseUrl: process.env.VUE_APP_BASE_API + '/common/upload',
      headerConfig: { Authorization: 'Bearer ' + getToken() },
      disabled: false,
      map: null,
      marker: null,
      lat: null,
      lng: null,
      natureCirculationArr: [],
      queryParamsNature: {
      },
      itemId: undefined
    }
  },
  created() {
    this.getList()
    // 获取流转性质
    this.getNatureData()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listPost(this.queryParams).then(response => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        title: undefined,
        contents: undefined,
        isshow: undefined,
        newsType: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加登记信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const postId = row.id || this.ids
      this.itemId = row.id
      getPost(postId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改登记信息'
        this.form.auditStatus = response.data.auditStatus.toString()
        this.form.state = response.data.state.toString()
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.itemId !== undefined) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delPost(postIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    chooseOpen() {
      this.openVisible = true
      this.$nextTick(() => {
        this.initMap()
      })
    },
    initMap(lat = 38.149402516518, lng = 117.50441804918) {
      const center = new window.TMap.LatLng(lat, lng)
      const container = this.$refs.mapView
      container.innerHTML = ''
      this.map = new window.TMap.Map(container, {
        center: center, // 设置地图中心点坐标
        zoom: 10, // 设置地图缩放级别
        baseMap: { // 设置卫星地图
          type: 'satellite'
        }
      })
      if (this.form.lat) {
        this.lat = this.form.lat
        this.lng = this.form.lng
        this.createMarker(this.form.lat, this.form.lng)
      }
      this.map.on('click', (event) => {
        this.lat = event.latLng.getLat()
        this.lng = event.latLng.getLng()
        // alert("经度:"+longitude+","+"纬度:"+latitude);
        this.createMarker(this.lat, this.lng)
      })
    },
    createMarker(lat, lng) {
      this.removeMarker()
      if (!this.marker) {
        this.marker = new window.TMap.MultiMarker({
          id: 'marker-layer',
          map: this.map,
          styles: {
            'marker': new window.TMap.MarkerStyle({
              'width': 25,
              'height': 35,
              'anchor': { x: 16, y: 32 }
            })
          },
          geometries: [{
            'id': 'demo',
            'styleId': 'marker',
            'position': new window.TMap.LatLng(lat, lng),
            'properties': {
              'title': 'marker'
            }
          }]
        })
      }
    },
    // 移除marker事件
    removeMarker() {
      if (this.marker) {
        this.marker.setMap(null)
        this.marker = null
      }
    },
    choosePosition() {
      this.form.lat = this.lat
      this.form.lng = this.lng
      this.openVisible = false
      this.lat = null
      this.lng = null
    },
    getNatureData() {
      getNature(this.queryParamsNature).then(response => {
        this.natureCirculationArr = response.data
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.map {
  width: 100%;
  position: relative;
  margin: 0 auto;
  .box {
    position: absolute;
    left: 10px;
    top: 20px;
    background: #fff;
    padding: 10px 15px;
    color: #333;
    font-size: 14px;
    z-index: 10;
  }
}

.nature-circulation {
  width: 100%;
}
</style>
